﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Pasys.PresentationCore
{
    public abstract class AbstractSettingViewProvider : ISettingViewProvider, IDisposable
    {

        public IWin32Window Onwer { get; set; }

        private Control SingleView;

        public bool IsPath { get; set; }
        public string ParentSettingKey { get; protected set; }
        public string SettingKey { get; protected set; }
        public string Title { get; protected set; }
        public bool Changed { get; protected set; }

        public void SetParentSettingKey(string parentSettingKey)
        {
            this.ParentSettingKey = parentSettingKey;
        }

        //public void SetSettingKey(string SettingKey)
        //{
        //    this.SettingKey = SettingKey;
        //}

        public void SetTitle(string title)
        {
            this.Title = title;
        }

        public virtual bool Save()
        {
            return true;
        }

        public AbstractSettingViewProvider()
            : this(false)
        {
        }

        public AbstractSettingViewProvider(bool isPath)
        {
            IsPath = isPath;
            ParentSettingKey = string.Empty;
            SettingKey = string.Empty;
            Title = string.Empty;
        }

        public Control View
        {
            get
            {
                if (!IsPath && SingleView == null)
                {
                    SingleView = CreateControl();
                    if (SingleView != null)
                    {
                        SingleView.Disposed += _view_Disposed;
                    }
                }
                return SingleView;
            }
        }

        public virtual object ViewInfo
        {
            get
            {
                return null;
            }
        }


        private void _view_Disposed(object sender, EventArgs e)
        {
        }

        protected abstract Control CreateControl();

        public void Dispose()
        {
            if (SingleView != null)
            {
                SingleView.Dispose();
                SingleView = null;
            }
        }
    }
}
